package actions.combat;

import bin.Mud;
import mobs.NPC;
import players.Player;

import java.util.Random;

public class CombatThread extends Thread {
    private Mud mudWindow;
    private boolean isFlee = false;
    private boolean inCombat;
    private NPC mob;
    private Player player;

    public CombatThread(Mud mudWindow, NPC mob, Player player) {
        this.mudWindow = mudWindow;
        this.mob = mob;
        this.player = player;
    }

    public void run() {
        inCombat = true;
        String mobName = mob.getName();
        int hp = player.getHealthPoints();
        int mobAttack = mob.getModifiedAttack();
        boolean stopThread = false;


        while (hp > 0 && !stopThread) {
            this.mudWindow.setReturnText("HP: " + hp);
            this.mudWindow.setReturnText(mobName + " hits you for " + mobAttack + " damage.");
            hp = hp - mobAttack;
            if (hp < 0) {
                hp = 0;
            }
            player.setHealthPoints(hp);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
                System.out.println(ex);
            }
            if (hp == 0) {
                this.mudWindow.setReturnText("HP: " + hp + "\nYou have died, you can collect your shiz at your corpse, if you can find it.");
                stopThread = true;
            }
            if (isFlee) {
                this.mudWindow.setReturnText("You tuck your tail between your legs and run away, like a little girl.");
                stopThread = true;
            }
        }
        inCombat = false;
    }

    public void setFlee() {
        isFlee = true;
    }

    public boolean inCombat() {
        return inCombat;
    }

    public boolean fleeSuccess() {
        Random random = new Random();
        int fleeChance = random.nextInt(100);

        return (fleeChance + player.getConfucius() >= 25);
    }
}